home *** CD-ROM | disk | FTP | other *** search
/ Windows Game Programming for Dummies (2nd Edition) / WinGamProgFD.iso / mac / DirectX SDK / DXSDK / samples / Multimedia / Misc / DXinstall / winmain.cpp < prev   
C/C++ Source or Header  |  2001-10-31  |  12KB  |  348 lines

  1. //-----------------------------------------------------------------------------
  2. // File: WinCode.cpp
  3. //
  4. // Desc: All of the Windows specific code needed for the DSetup sample
  5. //
  6. //      The code in this file includes the main Windows entry point
  7. //      as well as code to handle messages and our modeless version of
  8. //      MessageBox().
  9. //
  10. //      Call Tree:
  11. //         WinMain                      Main Windows Entry Point
  12. //            DirectXInstallInit        Initializes & registers window class
  13. //               DirectXInstallWndProc  Processes windows messages
  14. //                  DirectXInstall      See DINSTALL.CPP
  15. //                  DirectXGetVersion   See DINSTALL.CPP
  16. //         DirectXInstall               See DINSTALL.CPP
  17. //            DlgProc                   Handles all messages for our modeless MessageBox()
  18. //               SetButtons             Initializes the text of the dialog buttons to mimmic MessageBox()
  19. //                  ShowButton          Helper function to get and set the text of a button from resource strings
  20. //
  21. // Copyright (c) 1998-2001 Microsoft Corporation. All rights reserved.
  22. //-----------------------------------------------------------------------------
  23. #include <windows.h>
  24. #include <basetsd.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <dsetup.h>
  28. #include "resource.h"
  29. #include "DXInstall.h"
  30.  
  31.  
  32.  
  33.  
  34. //-----------------------------------------------------------------------------
  35. // Global variables
  36. //-----------------------------------------------------------------------------
  37. DWORD       g_dwStatus    = SHOW_ALL; // Filter setting for messages from DirectXSetup
  38. HINSTANCE   g_hInstance;              // Global instance handle
  39. HWND        g_hDlg        = NULL;     // Window handle to dialog proc
  40. CHAR        g_strAppTitle[256];       // Application title
  41. INT         g_iReply      = -1;       // Global value for dialog return
  42. BOOL        g_bCheckOlder = FALSE;    // Whether or not to check for older installs
  43.  
  44.  
  45.  
  46.  
  47. //-----------------------------------------------------------------------------
  48. // Name: ShowButton()
  49. // Desc: Helper function to get and set the text of a button from the
  50. //       resource strings.
  51. //-----------------------------------------------------------------------------
  52. VOID ShowButton( HWND hDlg, int Id, int strid )
  53. {
  54.     HWND btnHwd = GetDlgItem( hDlg, Id );
  55.     CHAR buf[20];
  56.  
  57.     LoadString( g_hInstance, strid, buf, 20 );
  58.     SetWindowText( btnHwd, buf );
  59.     ShowWindow( btnHwd, SW_NORMAL );
  60. }
  61.  
  62.  
  63.  
  64.  
  65. //-----------------------------------------------------------------------------
  66. // Name: SetButtons()
  67. // Desc: Initializes the text of the dialog buttons to mimmic MessageBox()
  68. //-----------------------------------------------------------------------------
  69. VOID SetButtons( HWND hDlg, DWORD wMsgType )
  70. {
  71.     LONG dwStyle;
  72.  
  73.     switch( wMsgType & 0x0000000F )
  74.     {
  75.         case MB_OKCANCEL:
  76.             ShowButton( hDlg, IDBUT1, STR_OK );
  77.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  78.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  79.             break;
  80.  
  81.         case MB_OK:
  82.             ShowButton( hDlg, IDBUT3, STR_OK );
  83.             break;
  84.  
  85.         case MB_RETRYCANCEL:
  86.             ShowButton( hDlg, IDBUT1, STR_RETRY );
  87.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  88.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  89.             break;
  90.  
  91.         case MB_ABORTRETRYIGNORE:
  92.             ShowButton( hDlg, IDBUT1, STR_ABORT );
  93.             ShowButton( hDlg, IDBUT3, STR_RETRY );
  94.             ShowButton( hDlg, IDBUT2, STR_IGNORE );
  95.             break;
  96.  
  97.         case MB_YESNOCANCEL:
  98.             ShowButton( hDlg, IDBUT1, STR_YES );
  99.             ShowButton( hDlg, IDBUT3, STR_NO );
  100.             ShowButton( hDlg, IDBUT2, STR_CANCEL );
  101.             break;
  102.  
  103.         case MB_YESNO:
  104.             ShowButton( hDlg, IDBUT1, STR_YES );
  105.             ShowButton( hDlg, IDBUT2, STR_NO );
  106.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  107.             break;
  108.  
  109.         default:
  110.             ShowWindow( GetDlgItem( hDlg, IDBUT1 ), SW_HIDE );
  111.             ShowWindow( GetDlgItem( hDlg, IDBUT2 ), SW_HIDE );
  112.             ShowWindow( GetDlgItem( hDlg, IDBUT3 ), SW_HIDE );
  113.             break;
  114.     }
  115.  
  116.     if( !(wMsgType & MB_DEFBUTTON2) )
  117.     {
  118.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT2 ), GWL_STYLE );
  119.         SendMessage( GetDlgItem( hDlg, IDBUT2 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  120.     }
  121.     else
  122.     {
  123.         dwStyle = GetWindowLong(GetDlgItem( hDlg, IDBUT2 ), GWL_STYLE);
  124.         SendMessage( GetDlgItem( hDlg, IDBUT2 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  125.     }
  126.  
  127.     if (!(wMsgType & MB_DEFBUTTON3))
  128.     {
  129.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT3 ), GWL_STYLE);
  130.         SendMessage( GetDlgItem( hDlg, IDBUT3 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  131.     }
  132.     else
  133.     {
  134.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT3 ), GWL_STYLE);
  135.         SendMessage( GetDlgItem( hDlg, IDBUT3 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  136.     }
  137.  
  138.     if (!(wMsgType & MB_DEFBUTTON3) && !(wMsgType & MB_DEFBUTTON2))
  139.     {
  140.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT1 ), GWL_STYLE);
  141.         SendMessage( GetDlgItem( hDlg, IDBUT1 ), BM_SETSTYLE, dwStyle | BS_DEFPUSHBUTTON, 0 );
  142.     }
  143.     else
  144.     {
  145.         dwStyle = GetWindowLong( GetDlgItem( hDlg, IDBUT1 ), GWL_STYLE );
  146.         SendMessage( GetDlgItem( hDlg, IDBUT1 ), BM_SETSTYLE, dwStyle & ~BS_DEFPUSHBUTTON, 0 );
  147.     }
  148. }
  149.  
  150.  
  151.  
  152.  
  153. //-----------------------------------------------------------------------------
  154. // Name: DlgProc()
  155. // Desc: Message proc for our modeless version of MessageBox()
  156. //       This function sets g_wReply for GetReply()
  157. //-----------------------------------------------------------------------------
  158. DLGPROC DlgProc( HWND hDlg, UINT msg, WPARAM wParam, LPARAM lParam )
  159. {
  160.     switch( msg )
  161.     {
  162.         case WM_INITDIALOG:
  163.             SetButtons( hDlg, -1 );
  164.             break;
  165.  
  166.         case WM_COMMAND:
  167.             switch( LOWORD(wParam) )
  168.             {
  169.                 case IDBUT1:
  170.                 case IDBUT2:
  171.                 case IDBUT3:
  172.                     // Let GetReply() know the user clicked on a button
  173.                     g_iReply = LOWORD(wParam);
  174.                     break;
  175.             }
  176.             break;
  177.         case WM_ACTIVATE:
  178.             if( LOWORD(wParam) == WA_INACTIVE )
  179.             {
  180.                 if( (HWND)lParam == GetParent( hDlg ) )
  181.                 {
  182.                     SetForegroundWindow( hDlg );
  183.                 }
  184.             }
  185.             break;
  186.     }
  187.  
  188.     return 0;
  189. }
  190.  
  191.  
  192.  
  193.  
  194. //-----------------------------------------------------------------------------
  195. // Name: SetStatusChecks()
  196. // Desc: Helper function to set checkmarks by status menu items
  197. //-----------------------------------------------------------------------------
  198. VOID SetStatusChecks( HWND hWnd )
  199. {
  200.     CheckMenuItem( GetMenu( hWnd ), IDSHOWALL,      MF_BYCOMMAND|MF_UNCHECKED );
  201.     CheckMenuItem( GetMenu( hWnd ), IDSHOWUPGRADES, MF_BYCOMMAND|MF_UNCHECKED );
  202.     CheckMenuItem( GetMenu( hWnd ), IDSHOWPROBLEMS, MF_BYCOMMAND|MF_UNCHECKED );
  203.     CheckMenuItem( GetMenu( hWnd ), IDSHOWNOTHING,  MF_BYCOMMAND|MF_UNCHECKED );
  204.  
  205.     switch( g_dwStatus )
  206.     {
  207.         case SHOW_ALL:
  208.             CheckMenuItem( GetMenu( hWnd ), IDSHOWALL, MF_BYCOMMAND|MF_CHECKED );
  209.             break;
  210.         case SHOW_UPGRADES:
  211.             CheckMenuItem( GetMenu( hWnd ), IDSHOWUPGRADES, MF_BYCOMMAND|MF_CHECKED );
  212.             break;
  213.         case SHOW_PROBLEMS:
  214.             CheckMenuItem( GetMenu( hWnd ), IDSHOWPROBLEMS, MF_BYCOMMAND|MF_CHECKED );
  215.             break;
  216.         case SHOW_NONE:
  217.             CheckMenuItem( GetMenu( hWnd ), IDSHOWNOTHING, MF_BYCOMMAND|MF_CHECKED );
  218.             break;
  219.     }
  220. }
  221.  
  222.  
  223.  
  224.  
  225. //-----------------------------------------------------------------------------
  226. // Name: DirectXInstallWndProc()
  227. // Desc: Processes windows messages
  228. //-----------------------------------------------------------------------------
  229. INT_PTR CALLBACK DirectXInstallWndProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  230. {
  231.     switch( msg )
  232.     {
  233.         case WM_COMMAND:
  234.             // Process menu items
  235.             switch( LOWORD(wParam) )
  236.             {
  237.                 case IDINSTALL:
  238.                     DirectXInstall( hWnd );
  239.                     break;
  240.                 case IDGETVERSION:
  241.                     DirectXGetVersion();
  242.                     break;
  243.                 case IDEXIT:
  244.                     DestroyWindow( hWnd );
  245.                     break;
  246.                 case IDSHOWALL:
  247.                     g_dwStatus = SHOW_ALL;
  248.                     SetStatusChecks( hWnd );
  249.                     break;
  250.                 case IDSHOWUPGRADES:
  251.                     g_dwStatus = SHOW_UPGRADES;
  252.                     SetStatusChecks( hWnd );
  253.                     break;
  254.                 case IDSHOWPROBLEMS:
  255.                     g_dwStatus = SHOW_PROBLEMS;
  256.                     SetStatusChecks( hWnd );
  257.                     break;
  258.                 case IDSHOWNOTHING:
  259.                     g_dwStatus = SHOW_NONE;
  260.                     SetStatusChecks( hWnd );
  261.                     break;
  262.                 case IDCHECKOLDERINSTALL:
  263.                     g_bCheckOlder = !g_bCheckOlder;
  264.                     CheckMenuItem( GetMenu(hWnd), IDCHECKOLDERINSTALL,
  265.                                                   MF_BYCOMMAND|MF_CHECKED );
  266.                     break;
  267.             }
  268.             return 0;
  269.  
  270.         case WM_DESTROY:
  271.             PostQuitMessage( 0 );
  272.             return 0;
  273.     }
  274.  
  275.     return DefWindowProc( hWnd, msg, wParam, lParam );
  276. }
  277.  
  278.  
  279.  
  280.  
  281. //-----------------------------------------------------------------------------
  282. // Name: DirectXInstallInit()
  283. // Desc: Initializes window data and registers window class
  284. //       Sets up a structure to register the window class.  Structure includes
  285. //       such information as what function will process messages, what cursor
  286. //       and icon to use, etc.
  287. //-----------------------------------------------------------------------------
  288. BOOL DirectXInstallInit( HINSTANCE hInstance )
  289. {
  290.     WNDCLASS wndClass;      // structure pointer
  291.     ZeroMemory( &wndClass, sizeof(WNDCLASS) );
  292.     wndClass.style         = CS_GLOBALCLASS;
  293.     wndClass.lpfnWndProc   = (WNDPROC) DirectXInstallWndProc;
  294.     wndClass.hInstance     = hInstance;
  295.     wndClass.hIcon         = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MAIN_ICON));
  296.     wndClass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  297.     wndClass.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  298.     wndClass.lpszMenuName  = "MainMenu";
  299.     wndClass.lpszClassName = (LPSTR) "DirectXInstall";
  300.  
  301.     // Returns result of registering the window
  302.     return RegisterClass( &wndClass );
  303. }
  304.  
  305.  
  306.  
  307.  
  308. //-----------------------------------------------------------------------------
  309. // Name: WinMain()
  310. // Desc: Calls initialization function, processes message loop
  311. //       This will initialize the window class if it is the first time this
  312. //       application is run.  It then creates the window, and processes the
  313. //       message loop until a PostQuitMessage is received.  It exits the
  314. //       application by returning the value passed by the PostQuitMessage.
  315. //-----------------------------------------------------------------------------
  316. int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, 
  317.                     LPSTR strCmdLine, int nCmdShow )
  318. {
  319.     // Has application been initialized?
  320.     if( NULL == hPrevInstance )
  321.         if( 0 == DirectXInstallInit( hInstance ) )
  322.             return 0;
  323.  
  324.     g_hInstance = hInstance;
  325.  
  326.     HWND hWnd = CreateWindow( "DirectXInstall", "DirectX Install",
  327.                               WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
  328.                               CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  329.                               NULL, NULL, g_hInstance, NULL );
  330.     if( NULL == hWnd )
  331.         return 0;
  332.  
  333.     LoadString( g_hInstance, STR_TITLE, g_strAppTitle, 200 );
  334.     ShowWindow( hWnd, SW_NORMAL );
  335.     UpdateWindow( hWnd );     // Send a WM_PAINT message
  336.     SetStatusChecks( hWnd );  // Check the default message menu item
  337.  
  338.     MSG msg;
  339.     while( GetMessage( &msg, NULL, 0, 0 ) )
  340.     {
  341.         TranslateMessage( &msg );
  342.         DispatchMessage( &msg );
  343.     }
  344.  
  345.     // Returns the value from PostQuitMessage
  346.     return (int)msg.wParam;    
  347. }
  348.